﻿using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;

using Gift.DAL;
using Gift.Model;
using Gift.WebApp;

namespace WebApp.cn.ashx
{
    public class goodsCommentHandler : IHttpHandler, IRequiresSessionState
    {

        readonly BasePage thePage = new BasePage();

        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        tbGoodsComment oGoodsComment = new tbGoodsComment();
     

        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {

            try
            {
                if (context.Session["user"] == null)
                {
                    context.Response.Write("0");
                }
                else if (string.Compare(context.Request.Form["code"], context.Session["tCheckCode"].ToString(), true) != 0)
                {
                    context.Response.Write("1");
                }
                else if (context.Request.Form["id"].Contains(","))
                {
                    ArrayList list = ArrayList.Adapter(context.Request.Form["id"].Split(new char[] { ',' }));
                    foreach (string str in list)
                    {
                        this.oGoodsComment.goods = Convert.ToInt32(str);
                        this.oGoodsComment.content = context.Request.Form["content"];
                        this.oGoodsComment.contact = context.Request.Form["contact"];
                        this.oGoodsComment.user = (context.Session["user"] as tbUser).id;
                        this.oGoodsComment.commentIP = new ClinetMAC().GetIPAddress();
                        BaseFactory.theGoodsComment.Add(this.oGoodsComment);
                    }
                    context.Response.Write("3");
                }
                else
                {
                    this.oGoodsComment.goods = Convert.ToInt32(context.Request.Form["id"]);
                    this.oGoodsComment.content = context.Request.Form["content"];
                    this.oGoodsComment.contact = context.Request.Form["contact"];
                    this.oGoodsComment.user = (context.Session["user"] as tbUser).id;
                    this.oGoodsComment.commentIP = new ClinetMAC().GetIPAddress();
                    if (BaseFactory.theGoodsComment.Add(this.oGoodsComment) > 0)
                    {
                        StringBuilder builder = new StringBuilder();
                        DataTable goodsCommentByGoodsAndTop = BaseFactory.theGoodsComment.GetGoodsCommentByGoodsAndTop(this.oGoodsComment.goods, 0);
                        builder.AppendLine("<ul class=\"comment_list\">");
                        foreach (DataRow row in goodsCommentByGoodsAndTop.Rows)
                        {
                            builder.AppendLine("<li>");
                            builder.AppendLine(string.Concat(new object[] { "<h5>", row["userName"], " <span>", Convert.ToDateTime(row["commentTime"]).ToString("yyyy-MM-dd HH:mm:ss"), "</span></h5>" }));
                            builder.AppendLine("<p>" + row["content"] + "</p>");
                            builder.AppendLine("</li>");
                        }
                        builder.AppendLine("</ul>");
                        context.Response.Write(builder.ToString());
                    }
                    else
                    {
                        context.Response.Write("2");
                    }
                }
            }
            catch
            {
                context.Response.Write("2");
            }
        }

        #endregion
    }
}
